package com.lujianfei.powersearch.ui.test

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import com.lujianfei.powersearch.R
import com.lujianfei.powersearch.base.BaseActivity
import com.lujianfei.powersearch.databinding.ActivityTestBinding
import com.lujianfei.powersearch.ui.createOrUpdate.widget.imageviewedit.adapter.ImageViewEditAdapter
import com.lujianfei.powersearch.ui.createOrUpdate.widget.imageviewedit.model.ImageViewEditItem
import com.lujianfei.powersearch.ui.sort.touchhelper.SortDragTouchCallback
import com.lujianfei.powersearch.ui.test.viewmodel.TestViewModel

class TestActivity: BaseActivity<ActivityTestBinding>() {

    companion object {
        fun start(context: Context) {
            context.startActivity(Intent(context, TestActivity::class.java))
        }
    }

    private val mTestViewModel: TestViewModel by lazy { buildViewModel() }

    override fun resourceId() = R.layout.activity_test

    override fun initView() {
        binding?.vm = mTestViewModel

        mTestViewModel.images.set("content://media/external/images/media/11432,content://media/external/images/media/11397")

        val mLinearLayoutManager = LinearLayoutManager(this)
        mLinearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
        val mAdapter = ImageViewEditAdapter()

        val images = "content://media/external/images/media/11432,content://media/external/images/media/11397"

        val sortDragTouchCallback = SortDragTouchCallback(mAdapter, verticalMove = false)
        val itemTouchHelper = ItemTouchHelper(sortDragTouchCallback)
        itemTouchHelper.attachToRecyclerView(binding?.recyclerview)

        mAdapter.setData(getData())
        binding?.recyclerview?.apply {
            layoutManager = mLinearLayoutManager
            adapter = mAdapter
        }
    }

    override fun initData() {
    }

    override fun initEvent() {
    }

    private fun getData():List<ImageViewEditItem> {
        val list = arrayListOf<ImageViewEditItem>()
        list.add(ImageViewEditItem(imageUri = Uri.parse("content://media/external/images/media/11432"), type = ImageViewEditItem.TYPE_NORMAL))
        list.add(ImageViewEditItem(imageUri = Uri.parse("content://media/external/images/media/11397"), type = ImageViewEditItem.TYPE_NORMAL))
        list.add(ImageViewEditItem(imageUri = null, type = ImageViewEditItem.TYPE_ADD))
        return list
    }
}